import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
// 导入一级路由
import IndexView from "../views/IndexView.vue";
import AddressView from "../views/AddressView.vue";
import LoginView from "../views/LoginView.vue";
import OrderView from "../views/OrderView.vue";
import ProductView from "../views/ProductView.vue";
import RegisterView from "../views/RegisterView.vue";
import ClassifySmallView from "../views/ClassifySmallView.vue";
import SubmitOrderView from "../views/SubmitOrderView.vue";
import AddressEditView from "../views/AddressEditView.vue"
import AddressChooseView from "../views/AddressChooseView.vue"
import SearchView from "../views/SearchView.vue";
import WalletView from "../views/WalletView.vue";
import OrderDetailsView from "../views/OrderDetailsView.vue";
import OrderManageView from "../views/OrderManage.vue";
//导入二级路由
import HomeView from "@/views/indexchild/HomeView.vue";
import CartView from "../views/indexchild/CartView.vue";
import UserView from "../views/indexchild/UserView.vue";
import ClassifyView from "../views/indexchild/ClassifyView.vue";



const routes: Array<RouteRecordRaw> = [
  // 根路由
	{path: '/', redirect: '/index/home'},
  {
    path: '/index',
    name: 'index', 
    component: IndexView,
    children:[
      {path: 'home', name: 'home', component: HomeView},
      {path: 'classify', name: 'classify', component: ClassifyView},
      {path: 'cart', name: 'cart', component: CartView},
      {path: 'user', name: 'user', component: UserView},
    ]
  },
  {path: '/address', name: "address", component: AddressView},
  {path: '/addressEdit', name: "addressEdit", component: AddressEditView},
  {path: '/addressChoose', name: "addressChoose", component: AddressChooseView},
  {path: '/ClassifySmall', name: "ClassifySmall", component: ClassifySmallView},
  {path: '/login', name: "login", component: LoginView},
  {path: '/order', name: "order", component: OrderView},
  {path: '/product', name: "product", component: ProductView},
  {path: '/register', name: "register", component: RegisterView},
  {path: '/submitOrder', name: "submitOrder", component: SubmitOrderView},
  {path: '/search', name: "search", component: SearchView},
  {path: '/wallet', name: "wallet", component: WalletView},
  {path: '/orderDetails', name: "orderDetails", component: OrderDetailsView},
  {path: '/orderManage', name: "orderManage", component: OrderManageView},
]

const Router = createRouter({
  history: createWebHashHistory(),
  routes
});

// 路由拦截
Router.beforeEach((to, from, next) => {
	let TOKEN = window.localStorage.getItem('token');
	// 如果是到 /index/cart  /index/user /address /order
	if((to.path == '/index/cart' || to.path == '/index/user' || to.path == '/address' || to.path == '/order') && !TOKEN){
		next('/login');
		return;
	};
	
	next();
});

export default Router;